package cn.elwy.rcp.ui.table.adapter;

import java.io.File;
import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.elwy.common.util.JsonUtil;
import cn.elwy.common.util.io.FileUtil;
import cn.elwy.osgi.config.ConfigContext;
import cn.elwy.osgi.config.ConfigPath;
import cn.elwy.rcp.ui.table.model.TableConfig;
import cn.elwy.rcp.ui.table.model.TableInfo;

public class TableConfigAdapter {

  private final Logger logger = LoggerFactory.getLogger(this.getClass());

  private static final String TABLE_CONFIG_EXT_NAME = "custom.dat";
  private static final String TABLE_INFO_EXT_NAME = "dat";

  public TableConfigAdapter() {
  }

  public TableConfig getTableConfig(Object subjectId, String tableId) {
    // 从本地文件加载个人配置信息
    TableConfig tableConfig = getTableConfigByFile(subjectId, tableId);
    if (tableConfig != null) {
      return tableConfig;
    }
    // 从服务器加载个人配置信息
    tableConfig = getTableConfigByDB(subjectId, tableId);
    if (tableConfig != null) {
      return tableConfig;
    }
    // 从本地文件加载全局配置信息
    tableConfig = getTableConfigByFile(null, tableId);
    if (tableConfig != null) {
      return tableConfig;
    }
    // 从服务器加载全局配置信息
    return getTableConfigByDB(null, tableId);
  }

  public void saveTableConfig(Object subjectId, TableConfig tableConfig) {
    saveTableConfigToFile(subjectId, tableConfig);
    saveTableConfigToDB(subjectId, tableConfig);
  }

  public TableInfo getTableInfo(Object subjectId, String tableId) {
    // 从本地文件加载个人配置信息
    TableInfo tableInfo = getTableInfoByFile(subjectId, tableId);
    if (tableInfo != null) {
      return tableInfo;
    }
    // 从服务器加载个人配置信息
    tableInfo = getTableInfoByDB(subjectId, tableId);
    if (tableInfo != null) {
      return tableInfo;
    }
    // 从本地文件加载全局配置信息
    tableInfo = getTableInfoByFile(null, tableId);
    if (tableInfo != null) {
      return tableInfo;
    }
    // 从服务器加载全局配置信息
    return getTableInfoByDB(null, tableId);
  }

  public void saveTableInfo(Object subjectId, TableInfo tableInfo) {
    saveTableInfoToFile(subjectId, tableInfo);
    saveTableInfoToDB(subjectId, tableInfo);
  }

  protected TableConfig getTableConfigByFile(Object subjectId, String tableId) {
    try {
      File file = getFile(subjectId, tableId, TABLE_CONFIG_EXT_NAME);
      if (!file.exists()) {
        return null;
      }
      String json = FileUtil.readFileToString(file.getAbsolutePath(), "UTF-8");
      return JsonUtil.toObject(json, TableConfig.class);
    } catch (Exception e) {
      logger.error(e.getMessage(), e);
    }
    return null;
  }

  protected TableConfig getTableConfigByDB(Object subjectId, String tableId) {
    return null;
  }

  protected void saveTableConfigToFile(Object subjectId, TableConfig tableConfig) {
    try {
      File file = getFile(subjectId, tableConfig.getId(), TABLE_CONFIG_EXT_NAME);
      String json = JsonUtil.toJson(tableConfig, true, "columnFormat");
      FileUtil.writeFileByString(file, json, "UTF-8");
    } catch (IOException e) {
      logger.error(e.getMessage(), e);
    }
  }

  protected void saveTableConfigToDB(Object subjectId, TableConfig tableConfig) {
  }

  protected TableInfo getTableInfoByFile(Object subjectId, String tableId) {
    try {
      File file = getFile(subjectId, tableId, TABLE_INFO_EXT_NAME);
      if (!file.exists()) {
        return null;
      }
      String json = FileUtil.readFileToString(file.getAbsolutePath(), "UTF-8");
      return JsonUtil.toObject(json, TableInfo.class);
    } catch (Exception e) {
      logger.error(e.getMessage(), e);
    }
    return null;
  }

  protected TableInfo getTableInfoByDB(Object subjectId, String tableId) {
    return null;
  }

  protected void saveTableInfoToFile(Object subjectId, TableInfo tableInfo) {
    try {
      File file = getFile(subjectId, tableInfo.getId(), TABLE_INFO_EXT_NAME);
      String json = JsonUtil.toJson(tableInfo);
      FileUtil.writeFileByString(file, json, "UTF-8");
    } catch (IOException e) {
      logger.error(e.getMessage(), e);
    }
  }

  protected void saveTableInfoToDB(Object subjectId, TableInfo tableInfo) {
  }

  public File getFile(Object subjectId, String tableId, String extName) {
    return ConfigPath.getFile(ConfigContext.getTableConfigDir(), subjectId, tableId, extName);
  }
}
